package ru.housewife.service.daymenus;

import org.hibernate.Query;
import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.DayMenuForm;

/**
 * removing day menu
 *
 * @author Alexander Troshanin
 */
public class RemoveDayMenu implements HibernateAction<Boolean> {

    private DayMenuForm dayMenuForm; // to be removed

    public RemoveDayMenu(DayMenuForm dayMenuForm) {
        this.dayMenuForm = dayMenuForm;
    }

    @Override
    public Boolean execute(Session session) {
        Query q = session.createQuery("delete from DayMenu where id=:dayMenuId and usersTable.id=:tableId");
        q.setLong("dayMenuId", dayMenuForm.getId());
        q.setLong("tableId", dayMenuForm.getUsersTable().getId());
        if (q.executeUpdate() == 0) {
            return false;
        }

        dayMenuForm.getUsersTable().removeDayMenu(dayMenuForm.getId());
        return true;
    }
    
}
